package easycloud.ecs.web.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import easycloud.ecs.common.domain.Provider;
import easycloud.ecs.common.domain.User;
import easycloud.ecs.web.exception.DomainNotFoundException;
import easycloud.ecs.web.service.ProviderService;

@Controller
@RequestMapping("provider")
public class ProviderController extends BaseController {
	private static final Log logger = LogFactory
			.getLog(ProviderController.class);

	@Inject
	private ProviderService providerService;

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public ModelAndView createProcess(
			@ModelAttribute("provider") @Valid Provider provider,
			BindingResult providerBindingResult, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		if (providerBindingResult.hasErrors()) {
			mav.setViewName("provider.create");
			return mav;
		}
		try {
			User user = getUser(request);
			provider.setUser(user);
			providerService.save(provider);
			mav.addObject("msg", "保存成功");
		} catch (Exception e) {
			mav.addObject("msg", "保存失败");
		}
		return mav;
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public ModelAndView createSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("provider.create");
		Provider provider = new Provider();
		mav.addObject("provider", provider);
		return mav;
	}

	@RequestMapping(value = "delete/{providerId}", method = RequestMethod.GET)
	public ModelAndView deleteProcess(
			@PathVariable("providerId") Integer providerId,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		Provider provider = providerService.getProvider(providerId);
		User user = getUser(request);
		try {
			if (provider.getUser().equals(user)) {
				providerService.delete(provider);
			}
			mav.addObject("msg", "删除成功");
		} catch (Exception e) {
			mav.addObject("msg", "删除失败");
		}
		return mav;
	}

	@RequestMapping(value = "{providerId}")
	public ModelAndView detail(@PathVariable("providerId") Integer providerId) {
		ModelAndView mav = new ModelAndView("provider.detail");
		Provider provider = providerService.getProvider(providerId);
		if (provider == null) {
			throw new DomainNotFoundException(Provider.class, providerId);
		}

		mav.addObject("provider", provider);
		return mav;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView listSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("provider.list");
		User user = getUserWithSession(request);
		mav.addObject("providers", user.getProviders());
		return mav;
	}
}
